// $Id: handlers.h 88 2011-10-17 18:00:11Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef HANDLERS_H_
#define HANDLERS_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/tree.h>

struct niortsp_session;

struct niortsp_request;

typedef int (*niortsp_handler_cb)(
		struct niortsp_session *session,
		struct niortsp_request *req, void *cbarg);

struct niortsp_handler_entry {
	char *method;
	niortsp_handler_cb cb;
	void *cbarg;
	RB_ENTRY(niortsp_handler_entry) entry;
};

RB_HEAD(niortsp_handler_tree, niortsp_handler_entry);

int niortsp_handler_entry_compare(
		struct niortsp_handler_entry *a,
		struct niortsp_handler_entry *b);

RB_PROTOTYPE(niortsp_handler_tree, niortsp_handler_entry, next,
		niortsp_handler_entry_compare);

struct niortsp_handlers {
	struct niortsp_handler_tree tree;
};

struct niortsp_handlers *niortsp_handlers_new(void);

void niortsp_handlers_free(struct niortsp_handlers *handlers);

int niortsp_handlers_add(struct niortsp_handlers *handlers,
		const char *method, niortsp_handler_cb cb, void *cbarg);

void niortsp_handlers_remove(struct niortsp_handlers *handlers,
		struct niortsp_handler_entry *entry);

struct niortsp_handler_entry *niortsp_handlers_find(
		struct niortsp_handlers *handlers, const char *method);

void niortsp_handlers_clear(struct niortsp_handlers *handlers);

#ifdef __cplusplus
}
#endif

#endif /* HANDLERS_H_ */
